package com.jd.interceptor;

import java.util.Map;

import com.jd.po.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class UserInterceptor extends AbstractInterceptor {

	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("In UserInterceptor");
		//获得session
		Map session = ActionContext.getContext().getSession();
		if( session.get("user")==null) {
			System.out.println("UserInterceptor return LOGIN");
			return Action.LOGIN;
		}
		String username = ((User) session.get("user")).getUsername();
		//判断用户合法性
		if(username == null || "".equals(username)) {
			//返回用户登陆页面
			System.out.println("UserInterceptor return LOGIN");
			return Action.LOGIN;
		} else {
			//进行下一步操作，没有拦截
			System.out.println("UserInterceptor invoke");
			return invocation.invoke();
		}
	}

}
